package com.wugui.datax.admin.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.wugui.datax.admin.entity.JobDatasource;
import com.wugui.datax.admin.service.DatasourceQueryService;
import com.wugui.datax.admin.service.JobDatasourceService;
import com.wugui.datax.admin.util.JdbcConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/JdbcDatasource/meta")
@Api(tags = "CooDataX数据源")
public class JobDatasourceV2Controller extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private JobDatasourceService jobJdbcDatasourceService;

    @Autowired
    private DatasourceQueryService datasourceQueryService;


    /**
     * 获取所有数据源
     *
     * @return
     */
    @ApiOperation("获取所有数据源")
    @GetMapping
    public R<List<JobDatasource>> selectAllDatasource() {
        return success(this.jobJdbcDatasourceService.selectAllDatasource());
    }


    /**
     * 根据数据源id获取可用模式或者可用表名
     * <p>
     * 具有显式模式数据库返回模式集合
     * 其他返回表名集合
     *
     * @param datasourceId
     * @return
     */
    @GetMapping({"/{datasourceId}"})
    @ApiOperation("根据数据源id获取可用模式或者可用表名")
    public R<List<String>> getTableNames(@PathVariable(name = "datasourceId") Long datasourceId, @PathVariable(name = "tableSchema", required = false) String tableSchema) throws IOException {
        JobDatasource datasource = jobJdbcDatasourceService.getById(datasourceId);
        //具有显式模式
        switch (datasource.getDatasource()) {
            case JdbcConstants.ORACLE:
            case JdbcConstants.POSTGRESQL:
            case JdbcConstants.SQL_SERVER:
                //返回模式
                return success(datasourceQueryService.getTableSchema(datasourceId));
        }
        //返回表名
        return success(datasourceQueryService.getTables(datasourceId, tableSchema));
    }

    /**
     * 根据数据源id、表名或者模式名，获取表下字段或者模式下表集合
     * @param datasourceId
     * @param tableOrschema
     * @return
     * @throws IOException
     */
    @GetMapping({"/{datasourceId}/{tableOrschema}"})
    @ApiOperation("根据数据源id、表名或者模式名，获取表下字段或者模式下表集合")
    public R<List<String>> getTableNamesBySchema(@PathVariable(name = "datasourceId") Long datasourceId, @PathVariable(name = "tableOrschema", required = false) String tableOrschema) throws IOException {
        JobDatasource datasource = jobJdbcDatasourceService.getById(datasourceId);
        //具有显式模式
        switch (datasource.getDatasource()) {
            case JdbcConstants.ORACLE:
            case JdbcConstants.POSTGRESQL:
            case JdbcConstants.SQL_SERVER:
                //返回模式
                return success(datasourceQueryService.getTables(datasourceId, tableOrschema));
        }
        return success(datasourceQueryService.getColumns(datasourceId, tableOrschema));
    }

    /**
     * 根据数据源id和表名获取所有字段
     *
     * @param datasourceId 数据源id
     * @param tableName    表名
     * @return
     */
    @GetMapping({"/{datasourceId}/{tableSchema}/{tableName}"})
    @ApiOperation("根据数据源id、模式名、表名获取所有字段")
    public R<List<String>> getColumns(@PathVariable(name = "datasourceId") Long datasourceId, @PathVariable(name = "tableName") String tableName) throws IOException {
        return success(datasourceQueryService.getColumns(datasourceId, tableName));
    }
}